package com.example.scale;

import android.content.Context;
import android.content.SharedPreferences;

// Class to save and load user data
public class SavedData {
	
	//public static SharedPreferences myData = PreferenceManager.getDefaultSharedPreferences(null);
	public SharedPreferences myData;
	@SuppressWarnings("unused")
	private Context context;
	private SharedPreferences.Editor editor;
	
	// Saved Values
	public boolean togglePlotValue;
	public int controlMethodValue;
	public boolean toggleHintsValue;
	public boolean levelCompleteArray[];
	public int levelPowerupsCollected[];
	public int playerExperience;
	//public static String name;
	
	public SavedData(Context context) {
		this.context = context;
		myData = context.getSharedPreferences("com.example.scale", 0);
		editor = myData.edit();
	}
	
	public void loadPrefData() {
		togglePlotValue = myData.getBoolean("TOGGLE_PLOT", true); // Load toggle plot data, default value of true
		controlMethodValue = myData.getInt("CONTROL_METHOD", 2);
		toggleHintsValue = myData.getBoolean("TOGGLE_HINTS", true);
		//name = myData.getString("NAME", "");
	}
	
	public void loadProgressData() {
		// The player's experience number
		playerExperience = myData.getInt("PLAYER_EXPERIENCE", 0);
		
		// Array to store which levels have been completed
		levelCompleteArray = new boolean[LevelSelectScene.numLevels + 1];
		levelCompleteArray[0] = myData.getBoolean("LEVEL_COMPLETE"+0, true);
		for(int i = 2; i <= LevelSelectScene.numLevels; i++) {
			levelCompleteArray[i-1] = myData.getBoolean("LEVEL_COMPLETE"+(i-1), false);
		}
		
		// Array to store how many powerups were collected in each level
		levelPowerupsCollected = new int[LevelSelectScene.numLevels];
		for(int i=0; i< LevelSelectScene.numLevels; i++) {
			levelPowerupsCollected[i] = myData.getInt("LEVEL_POWERUPS_COLLECTED"+(i+1), 0);
		}
	}
	
	public void saveData(String key, boolean value) {
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	public void saveData(String key, int value) {
		editor.putInt(key, value);
		editor.commit();
	}
	
	public void saveData(String key, String value) {
		editor.putString(key, value);
		editor.commit();
	}
}
